<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="45%"
      v-dialogDrag
      append-to-body
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        v-loading="formLoading"
        label-width="100px"
      >
        <el-form-item label="房屋标签" prop="houseId">
          <el-select v-model="formData.houseId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.houseName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租赁信息图片地址" prop="imageUrl">
          <el-input
            v-model="formData.imageUrl"
            placeholder="请输入租赁信息图片地址"
          />
        </el-form-item>
        <el-form-item label="租赁信息标题" prop="title">
          <el-input v-model="formData.title" placeholder="请输入租赁信息标题" />
        </el-form-item>
        <el-form-item label="用户标签（性别/星座/职业）" prop="userTag">
          <el-input
            v-model="formData.userTag"
            placeholder="请输入用户标签（性别/星座/职业）"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as HouseInfoApi from '@/api/system/houseinfo';
import * as RentalsApi from "@/api/system/rentals";
export default {
  name: "RentalsForm",
  components: {},
  data() {
    return {
      options: {},
       
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        value: "",
        houseId: '',
        id: undefined,
        houseId: undefined,
        imageUrl: undefined,
        title: undefined,
        userTag: undefined,
      },
      queryParams: {
        offset: 1,
         size: 10,
                    pageNo: 1,
            pageSize: 10,
        houseId: null,
        imageUrl: null,
        title: null,
        userTag: null,
        
        createTime: [],
      },
      // 表单校验
      formRules: {
        houseId: [
          { required: true, message: "房屋ID不能为空", trigger: "blur" },
        ],
        imageUrl: [
          {
            required: true,
            message: "租赁信息图片地址不能为空",
            trigger: "blur",
          },
        ],
        title: [
          { required: true, message: "租赁信息标题不能为空", trigger: "blur" },
        ],
        userTag: [
          {
            required: true,
            message: "用户标签（性别/星座/职业）不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getListone();
  },
  methods: {
    async getListone() {
      try {
      this.loading = true;
         const res = await HouseInfoApi.getHouseInfoPage(this.queryParams);
      
        this.options = res.data.records;
        console.log(this.options);
      } finally {
        this.loading = false;
      }
    },
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await RentalsApi.getRentals(id);
          this.formData = res.data;
          this.title = "修改房屋租户信息";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增房屋租户信息";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        console.log(this.formData);
        // 修改的提交
        if (data.id) {
          await RentalsApi.updateRentals(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit("success");
          return;
        }
        // 添加的提交
        await RentalsApi.createRentals(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit("success");
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        houseId: undefined,
        imageUrl: undefined,
        title: undefined,
        userTag: undefined,
      };
      this.resetForm("formRef");
    },
  },
};
</script>
